﻿using UnityEngine;
using System.Collections;

public class HudUI : MonoBehaviour {
	
	
	public int mySID;
	public Team curTeamShowing;
	public bool isFirstTimeShow;
	
	
	//unit panel
	public GameObject MPLabel;
	public GameObject mainCharMP;
	public GameObject[] charHPObj;
	public GameObject[] unitImage;
	public GameObject[] unitImageBack;
	//public GameObject[] unitMoveBonus;
	public MoveBonusNum movebonusPrefab;
	public GameObject[] unitMoveBonusLabel;
	public Transform[] unitMoveBonusTrans;
	public Transform[] unitMoveBonusTwoDigitPos;
	public GameObject[] unitMoveBonusOne;
	public GameObject [] hpChangeLabel;	
	public Transform [] hpChangeLabelTrans;	
	public GameObject  hpChangePrefab;
	public GameObject bottomLeftAnchor;
	public GameObject topRightAnchor;
	public GameObject unitPanel;
	
	public GameObject trailPrefab;
	public Sprite [] iconMoveBonus;
	public void ChangeMPBar(int mp, int maxmp)
	{
		MPLabel.GetComponent<UILabel>().text = "MP "+mp.ToString();
		float fillAmount = mp/(float)maxmp;
		mainCharMP.GetComponent<UISprite>().fillAmount = fillAmount;		
	}

	
	public void ChangeHPBar(Team team, int index)
	{
		float fillAmount = (float)team.unit[index].hp/(float)team.unit[index].maxHp;
		glog.debug(index+"  fillamount="+fillAmount+"  "+team.unit[index].hp+"  "+team.unit[index].maxHp);
		charHPObj[index].GetComponent<UISprite>().fillAmount = fillAmount;
		if(team.unit[index].hp<=0) 
		{
			team.unit[index].hpChange = 0;
			StartCoroutine(DeathUnit(team, index));
		}
	}	
	
	public void HealUnitEffect(int index, int hpChange)
	{
		GameObject effGo = EffectManager.Instance.NGUICreateEffect(7, unitImage[index]);
		Destroy(effGo,3.0f);
		
		hpChangeLabel[index] = NGUITools.AddChild(unitPanel, hpChangePrefab);
		hpChangeLabel[index].transform.localPosition = hpChangeLabelTrans[index].localPosition;
		hpChangeLabel[index].transform.localScale = hpChangeLabelTrans[index].localScale;
		UILabel lbl = hpChangeLabel[index].GetComponent<UILabel>();
		lbl.text = hpChange.ToString();
		lbl.color = new Color(10/255.0f, 234/255.0f, 68/255.0f, 1);
		StartCoroutine(HealDamageNumFadeOut(lbl));
		//BoardManager.Instance.RestartFSM(hpChangeLabel[index].GetComponent<PlayMakerFSM>());
	}
	
	public void DamageUnitEffect(int index, int hpChange)
	{
		GameObject effGo = EffectManager.Instance.NGUICreateEffect(6, unitImage[index]);
		Destroy(effGo,3.0f);
		
		BoardManager.Instance.RestartFSM(unitImage[index].GetComponent<PlayMakerFSM>());
		
		hpChangeLabel[index] = NGUITools.AddChild(unitPanel, hpChangePrefab);
		hpChangeLabel[index].transform.localPosition = hpChangeLabelTrans[index].localPosition;
		hpChangeLabel[index].transform.localScale = hpChangeLabelTrans[index].localScale;		

		UILabel lbl = hpChangeLabel[index].GetComponent<UILabel>();
		if(hpChange<0)
			hpChange = -hpChange;
		lbl.text = hpChange.ToString();
		lbl.color = new Color(253/255.0f, 34/255.0f, 34/255.0f, 1);
		StartCoroutine(HealDamageNumFadeOut(lbl));
		//BoardManager.Instance.RestartFSM(hpChangeLabel[index].GetComponent<PlayMakerFSM>());
		//StartCoroutine("testHide");
	}
	
	IEnumerator HealDamageNumFadeOut(UILabel lbl)
	{
		yield return new WaitForSeconds(0.2f);
		Vector3 newPos = lbl.gameObject.transform.localPosition;
		newPos.y += 20;
		iTween.MoveTo( lbl.gameObject, iTween.Hash("time", 1.2f, "position", newPos, "islocal",true) );		
		
		yield return new WaitForSeconds(0.4f);
		float alpha = 1.0f;
		Color original = lbl.color;
		
		while(alpha>0)
		{
			//glog.debug("************ pos="+lbl.gameObject.transform.localPosition);
			alpha -= 2*Time.deltaTime;
			if(alpha<0)
				alpha = 0;
			lbl.color = new Color(original.r, original.g, original.b, alpha);
			yield return null;
		}
		Destroy(lbl.gameObject);
	}
	
//	IEnumerator testHide()
//	{
//		yield return new WaitForSeconds(0.3f);
//		EnemyUnitUI.Instance.HidePanel();
//	}
	
	public void MakeLiveUnit(Team team, int index)
	{
		if(curTeamShowing == team)
			unitImageBack[index].GetComponent<UISprite>().depth = 1;
		charHPObj[index].GetComponent<UISprite>().fillAmount = 1.0f;
		team.unit[index].isDead = false;		
	}
	
	public void MakeDeadUnit(Team team, int index)
	{
		if(curTeamShowing == team)
			unitImageBack[index].GetComponent<UISprite>().depth = 3;
		team.unit[index].isDead = true;
	}
	
	IEnumerator DeathUnit(Team team, int index)
	{
		yield return new WaitForSeconds(0.3f);
		GameObject effGo = EffectManager.Instance.NGUICreateEffect(8, unitImage[index]);
		Destroy(effGo,3.0f);
		MakeDeadUnit(team, index);
		
	}
	
	public void ShowMoveBonus(BoardPerson person, Transform destTrans, int index, string prefix, int nAddNum, bool isAnchorTopRight, DiceShapeType diceShape)
	{
		//StartCoroutine(MoveBonus(index, prefix, nAddNum));
		MoveBonus(person, destTrans, index, prefix, nAddNum, isAnchorTopRight, diceShape);
	}
	
	IEnumerator MoveTrailToPerson(GameObject trailGo, Vector3 screenPos)
	{
		float x = CSceneManager.widthScreen * screenPos.x/(float)Screen.width;
		float y = CSceneManager.heightScreen * screenPos.y/(float)Screen.height;
		Vector3 destPos = new Vector3( x, y, trailGo.transform.localPosition.z);
		//float sumTime = 0.0f;
		glog.debug("trailgo localpos="+trailGo.transform.position);
		iTween.MoveTo( trailGo, iTween.Hash("time", 0.4f, "position", destPos,   "islocal", true, "easetype","easeInSine" )  );
		while(!BoardManager.Instance.isBoardPersonMoving && (trailGo.transform.localPosition-destPos).sqrMagnitude>0.1f)
		{
			//sumTime += Time.deltaTime;
			//trailGo.transform.localPosition = Vector3.Lerp( trailGo.transform.localPosition, destPos, 5*Time.deltaTime);
			yield return null;
		}
		//trailGo.GetComponent<ParticleSystem>().Stop();
		Destroy(trailGo);//,2.0f);
		//trailGo.GetComponent<ParticleSystem>().Stop();
		//yield return new WaitForSeconds(0.5f);
		//Destroy(trailGo);
	}
	
	void MoveBonus(BoardPerson person, Transform destTrans, int index, string prefix, int nAddNum, bool isAnchorTopRight, DiceShapeType diceShape)
	{
		GameObject cardObj = null;
//		if(index==5)
//		{
//			glog.debug("dont show slot 5 bonus");
//			return;
//		}
//		else		
//			cardObj = CardManager.Instance.GetCardObject(index);
		
		
		cardObj = CardManager.Instance.GetCardObject(person.SID, index);
		
		if(cardObj==null)
		{
			Debug.LogError(person.SID+"  cardobj null "+index+"   nAddNum="+nAddNum);
			return;
		}
		//unitMoveBonus[index] = NGUITools.AddChild(cardObj, movebonusPrefab[0]);
		SoundManager.Instance.PlaySound(3);
		MoveBonusNum unitMoveBonus = ObjectPool.Spawn (movebonusPrefab) as MoveBonusNum;

		//SpriteRenderer spriteRen = unitMoveBonus.GetComponentInChildren<SpriteRenderer>();
		//spriteRen.sprite = iconMoveBonus[(int)diceShape];
		unitMoveBonus.transform.FindChild("wonhoFrame").gameObject.SetActive(true);
		if(diceShape== DiceShapeType.Normal)
			unitMoveBonus.transform.FindChild("wonhoFrame").gameObject.GetComponent<UISprite>().spriteName = "icon_circle_move";
		else if(diceShape== DiceShapeType.Attacker)
			unitMoveBonus.transform.FindChild("wonhoFrame").gameObject.GetComponent<UISprite>().spriteName = "icon_circle_attack";
		else if(diceShape== DiceShapeType.Defender)
			unitMoveBonus.transform.FindChild("wonhoFrame").gameObject.GetComponent<UISprite>().spriteName = "icon_circle_defence";

		unitMoveBonus.transform.parent = cardObj.transform;
		unitMoveBonus.transform.localPosition = movebonusPrefab.transform.localPosition;
		unitMoveBonus.transform.localScale = movebonusPrefab.transform.localScale;
		//unitMoveBonus.GetComponent<UILabel>().text = nAddNum.ToString();
		if(diceShape== DiceShapeType.Normal)
			unitMoveBonus.GetComponent<UISprite>().spriteName = "No_E"+nAddNum.ToString();
		else if(diceShape== DiceShapeType.Attacker)
			unitMoveBonus.GetComponent<UISprite>().spriteName = "No_C"+nAddNum.ToString();
		else if(diceShape== DiceShapeType.Defender)
			unitMoveBonus.GetComponent<UISprite>().spriteName = "No_D"+nAddNum.ToString();

		//Destroy(unitMoveBonus[index], 1.0f);
		
		GameObject effGo = EffectManager.Instance.NGUICreateEffect(5, cardObj);
		Destroy(effGo,3.0f);

		//GameObject trailGo = NGUITools.AddChild(cardObj, trailPrefab);
		//trailGo.transform.localPosition = trailPrefab.transform.localPosition;
		StartCoroutine( MoveNumTest(person, unitMoveBonus, index,	destTrans,  isAnchorTopRight) );

		
	}
	

	IEnumerator MoveNumTest(BoardPerson person, MoveBonusNum bonusNum, int index, Transform destTrans, bool isAnchorTopRight)
	{
		
		Vector3 destTransPos = destTrans.position;
			
		yield return null;
		
		
		//if(!isAnchorTopRight)
		
		
		yield return new WaitForSeconds(0.4f);
		
		yield return new WaitForSeconds(IngameSetting.Instance.MoveBonusDelay);
		
		bonusNum.transform.parent = bottomLeftAnchor.transform;

		Vector3 screenPos = Camera.main.WorldToScreenPoint(destTransPos);
		float x = CSceneManager.widthScreen * screenPos.x/(float)Screen.width;
		float y = CSceneManager.heightScreen * screenPos.y/(float)Screen.height;
		
		
//		if(isAnchorTopRight)
//		{
//			x = -CSceneManager.widthScreen * (1 - screenPos.x/(float)Screen.width );
//			y = -CSceneManager.heightScreen * (1 - screenPos.y/(float)Screen.height );
//		}
		bonusNum.transform.FindChild("wonhoFrame").gameObject.SetActive(false);
		
		Vector3 destPos = new Vector3( x, y, bonusNum.transform.localPosition.z);
		//float sumTime = 0.0f;
		glog.debug(destPos+ "  trailgo localpos="+bonusNum.transform.localPosition);
		//Vector3 originalPos = go.transform.position;
		//iTween.MoveTo( bonusNum.gameObject, iTween.Hash("time", 0.3f, "position", destPos,   "islocal", true, "easetype","easeInSine" )  );
		LeanTween.moveLocal( bonusNum.gameObject, destPos, 0.3f).setEase(LeanTweenType.easeInSine);

		/*while(!BoardManager.Instance.isBoardPersonMoving && (bonusNum.transform.localPosition-destPos).sqrMagnitude>0.01f)
		{
			//sumTime += Time.deltaTime;
			//trailGo.transform.localPosition = Vector3.Lerp( trailGo.transform.localPosition, destPos, 5*Time.deltaTime);
			yield return null;
		}	*/	
		
		yield return new WaitForSeconds(0.32f);
		UIManager.Instance.DiceNumEffect(person, destTransPos);
		
		
		//glog.debug("------ goto original pos "+index+"  "+go.transform.position+"  "+originalPos);
		//go.transform.position = originalPos;
		//Destroy(go);
		ObjectPool.Recycle( bonusNum);
	}

	public Vector3 FindScrPos(Vector3 pos, MoveDir anchor)
	{
		Vector3 screenPos = Camera.main.WorldToScreenPoint(pos);
		float x = CSceneManager.widthScreen/2.0f;// * screenPos.x/(float)Screen.width;
		float y = CSceneManager.heightScreen/2.0f;// * screenPos.y/(float)Screen.height;	
		if(anchor==MoveDir.TopRight)
		{
			x = -CSceneManager.widthScreen * (1 - screenPos.x/(float)Screen.width );
			y = -CSceneManager.heightScreen * (1 - screenPos.y/(float)Screen.height );			
		}
		return new Vector3(x,y,0);
	}

	public void SaveOriginalMoveBonusPos(float delay)
	{
//		if(!isFirstTimeShow)
//		{
//			isFirstTimeShow = true;
//			StartCoroutine(OriginalMoveBonusPos(delay));
//		}		
	}
	
//	IEnumerator OriginalMoveBonusPos(float delay)
//	{
//		yield return new WaitForSeconds(delay);
//		for(int index=0; index<curTeamShowing.unit.Length; index++)
//		{
//			unitMoveBonusPos[index] = unitMoveBonusLabel[index].transform.position;
//			glog.debug("unitMoveBonusPos "+index+"  "+unitMoveBonusPos[index]);
//		}
//	}	
}
